package main

import (
	"fmt"

	"github.com/garyburd/redigo/redis"
)

//https://www.jianshu.com/p/aeb27a6aa42d
func main() {
	c, err := redis.Dial("tcp", "120.26.95.197:6379")
	if err != nil {
		fmt.Println("Connect to redis error", err)
		return
	}
	defer c.Close()

	//写5秒过期
	_, err = c.Do("SET", "bbq", `{"cc":99}`, "EX", "5")
	if err != nil {
		fmt.Println("redis set failed:", err)
	}

	json, err := redis.String(c.Do("GET", "bbq"))
	if err != nil {
		fmt.Println("redis get failed:", err)
	} else {
		fmt.Printf("Get mykey: %v \n", json)
	}

	//检查值是否存在
	is_key_exit, err := redis.Bool(c.Do("EXISTS", "bbq"))
	if err != nil {
		fmt.Println("error:", err)
	} else {
		fmt.Printf("exists or not: %v \n", is_key_exit)
	}
}
